var behaviorsbasiclist = require('../../behavior/basiclist')
var addrbill = require('../../../sys/sys_addr.js')
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  behaviors: [behaviorsbasiclist],
  /**
   * 组件的属性列表
   */
  properties: {
    delivery_config: {
      type: Object,
      value: null
    },
    mode: {
      type: Number,
      default: 2
    },
    themcolor: {
      type: Object,
      default: {}
    },
    showselected: {
      type: Boolean,
      default: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    pagelist: [],
    isshow: false,
    op: "",
    navCur: 0,
    modellist: [],
    CustomBar: 0
  },

  ready() {
    //   
    var app = getApp();

    this.setData({
      CustomBar: app.globalData.CustomBar
    })
    this.data.actbill = addrbill

  },
  /**
   * 组件的方法列表
   */
  methods: {
    // chooseLocation(ev){



    //   var addresslist = this.selectComponent("#addresslist");
    //   var addform = addresslist.selectComponent("#addform")

    //      if(addform)
    //      {
    //        addform.chooseLocation(ev)
    //      }


    //  },
    showaddresslist() {
      console.log("showaddresslist")
      console.log("dddddddddddddd")

      this.setData({
        isshow: true
      })
      this.xcshow(1)
    },
    xcshow(navCur) {

      this.getpagelist(1);
      this.setData({
        isshow: true,
        navCur
      })
    },
    Backfunc() {
      this.setData({
        isshow: false
      })
    },
    hideform() {
      this.setData({
        op: ""
      })
    },

    selecteditem(e) {
      if (this.data.mode == 2) {
        var dataset = e.currentTarget.dataset;
        var selectaddr = this.data.pagelist[dataset["idx"]];
        var app = getApp();
        if (this.data.navCur == 3) {
          if (selectaddr.lat == 0 || !selectaddr.lat) {
            wx.showToast({
              title: '请先完善信息',
            })

            return;
          } else {
            var intracity = this.data.delivery_config.intracity;
            selectaddr["distance"] = app.toolv2.distanceByLnglat(selectaddr.lng, selectaddr.lat, intracity.lng, intracity.lat)
            if (selectaddr["distance"].val - intracity["maxdistance"] > 0) {
              wx.showToast({
                title: '配送范围之外',
              })
              return;
            }

          }
        }

        app.globalData.addr = selectaddr;
        this.setData({
          selectaddr: selectaddr,
          op: "result",
          isshow: false
        })

        var myEventDetail = selectaddr // detail对象，提供给事件监听函数
        var myEventOption = {} // 触发事件的选项
        this.triggerEvent("selectaddr", myEventDetail, myEventOption)
      }
    },
    addreditblack(e) {
      var addrmodel = e.detail
      var op = this.data.op;
      var that = this;
      var pagelist = that.data.pagelist
      if (op == "add") {
        pagelist.splice(0, 0, addrmodel)
      }
      if (addrmodel.isdefault == 1) {
        for (var a in pagelist) {
          if (pagelist[a].id != addrmodel.id) {
            pagelist[a].isdefault = -1
          }
        }
      }
      var updatemodel = {}
      updatemodel["pagelist"] = pagelist;
      updatemodel["op"] = "";
      this.setData(updatemodel)
    },
    // 选择编辑导入增加地址
    optool(e) {
      var dataset = e.currentTarget.dataset;
      var addform = this.selectComponent("#addform");
      var idx = dataset.idx;
      var op = dataset.op
      var that = this;
      var updatemodel = {};
      switch (op) {
        case "del": {
          var model = this.data.pagelist[idx];
          var that = this;
          wx.showModal({
            title: '删除',
            content: "删除 " + model.name + "[" + model.phone + "]",
            showCancel: true,
            success(res) {
              if (res.confirm) {
                addrbill.del(model.id).then(function (vdata) {
                  if (vdata["status"] == 1) {

                    that.data.pagelist.splice(idx, 1);

                    that.setData({
                      pagelist: that.data.pagelist
                    });
                    wx.showToast({
                      title: '删除成功'
                    })
                  }
                })

              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          break;

        }
        case "edit": {
          addform.showop(dataset.op, this.data.pagelist[idx]);
          updatemodel["acteq"] = idx;
          updatemodel["op"] = op;
          this.setData(updatemodel)
          break;
        }
        case "add": {
          if (dataset.form == "choose") {
            wx.chooseAddress({
              success: function (res) {
                addform.showop(dataset.op, null, res);
                updatemodel["op"] = dataset.op;
                that.setData(updatemodel)
              }
            })
          } else {
            addform.showop(dataset.op, null);
            updatemodel["op"] = dataset.op;
            this.setData(updatemodel)
          }
          break;
        }
        case "setdefault": {


          var selectmodel = this.data.pagelist[idx];

          addrbill.setdefalut(selectmodel.id, -selectmodel.isdefault).then(function (params) {
            if (params["status"] == 1) {}
          })
          for (var ia in this.data.pagelist) {
            if (this.data.pagelist[ia].isdefault == 1) {
              updatemodel["pagelist.[" + ia + "].isdefault"] = -1;
              break;
            }
          }
          updatemodel["pagelist.[" + idx + "].isdefault"] = -selectmodel.isdefault;
          this.setData(updatemodel)
          break;
        }
        case "close": {
          this.setData({
            op: "list"
          })
          break;
        }
      }
    },
  }
})